package org.dromara.kitchen.controller;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.product.ProductRule;
import org.dromara.kitchen.domain.vo.ProductRuleVO;
import org.dromara.kitchen.service.IProductRuleService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.kitchen.domain.product.ProductRuleRelation;
import org.dromara.kitchen.service.IProductRuleRelationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 规格和商家服务类型
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/product/rule/relation")
public class ProductRuleRelationController extends BaseController {

    private final IProductRuleRelationService productRuleRelationService;
    private final IProductRuleService productRuleService;

    /**
     * 查询规格和商家服务类型列表
     */
//    @SaCheckPermission("kitchen:ruleRelation:list")
    @GetMapping("/list")
    public TableDataInfo<ProductRuleRelation> list(ProductRuleRelation productRuleRelation, PageQuery pageQuery) {
        QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
        if(productRuleRelation.getCateChildId() != null){
            qw.eq("cate_child_id",productRuleRelation.getCateChildId());
        }

        TableDataInfo<ProductRuleRelation> td = PageUtils.buildDataInfo(productRuleRelationService.page(PageUtils.buildPage(),qw));
        List<ProductRuleRelation> list = td.getRows();
        return td;
    }

    /**
     * 导出规格和商家服务类型列表
     */
//    @SaCheckPermission("kitchen:ruleRelation:export")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductRuleRelation productRuleRelation, HttpServletResponse response) {
        List<ProductRuleRelation> list = productRuleRelationService.list();
        ExcelUtil.exportExcel(list, "规格和商家服务类型", ProductRuleRelation.class, response);
    }

    /**
     * 获取规格和商家服务类型详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("kitchen:ruleRelation:query")
    @GetMapping("/{id}")
    public R<ProductRuleRelation> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(productRuleRelationService.getById(id));
    }


    /**
     * 查询规格和商家服务类型列表
     */
    @GetMapping("/getTransferData")
    public R<Map<String,Object>> getTransferData(@Validated ProductRuleRelation productRuleRelation) {
        QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
        if(productRuleRelation.getCateId() != null){
            qw.eq("cate_id",productRuleRelation.getCateId());
        }
        if(productRuleRelation.getCateChildId() != null){
            qw.eq("cate_child_id",productRuleRelation.getCateChildId());
        }

        Map<String, Object> map = new HashMap<>();
        List<Integer> valueRuleIds = new ArrayList<>();
        List<ProductRuleVO> valueRuleList = new ArrayList<>();
        List<ProductRuleRelation> list = productRuleRelationService.list(qw);
        if(CollUtil.isNotEmpty(list)){
            for (ProductRuleRelation relation : list){
                ProductRule rule = productRuleService.getById(relation.getRuleId());
                if(rule != null){
                    ProductRuleVO ruleVO = new ProductRuleVO();
                    ruleVO.setRuleId(rule.getId());
                    ruleVO.setRuleName(rule.getRuleName());
                    valueRuleIds.add(rule.getId());
                    valueRuleList.add(ruleVO);
                }
            }
        }
        map.put("valueRuleList", valueRuleList);
        map.put("valueRuleIds", valueRuleIds);

        List<ProductRuleVO> allRuleList = new ArrayList<>();
        List<ProductRule> allDataList = productRuleService.list();
        if(CollUtil.isNotEmpty(allDataList)){
            for (ProductRule rule : allDataList){
                ProductRuleVO ruleVO = new ProductRuleVO();
                ruleVO.setRuleId(rule.getId());
                ruleVO.setRuleName(rule.getRuleName());
                allRuleList.add(ruleVO);
            }
        }
        map.put("allRuleList", allRuleList);
        return R.ok(map);
    }


    /**
     * 新增规格和商家服务类型
     */
    @SaCheckPermission("kitchen:ruleRelation:add")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductRuleRelation productRuleRelation) {
        ProductRule productRule = productRuleService.getById(productRuleRelation.getRuleId());
        if(productRule!= null){
            productRuleRelation.setRuleName(productRule.getRuleName());
        }
        return toAjax(productRuleRelationService.save(productRuleRelation));
    }

    /**
     * 修改规格和商家服务类型
     */
    @SaCheckPermission("kitchen:ruleRelation:edit")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductRuleRelation productRuleRelation) {

        ProductRule productRule = productRuleService.getById(productRuleRelation.getRuleId());
        if(productRule!= null){
            productRuleRelation.setRuleName(productRule.getRuleName());
        }
        return toAjax(productRuleRelationService.updateById(productRuleRelation));
    }

    /**
     * 删除规格和商家服务类型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:ruleRelation:remove")
    @Log(title = "规格和商家服务类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productRuleRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


    /**
     * 删除规格和商家服务类型
     */
    @SaCheckPermission("kitchen:ruleRelation:remove")
    @Log(title = "规格和商家服务类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/removeByRuleId")
    public R<Void> removeByRuleId(@RequestBody ProductRuleRelation productRuleRelation) {
        QueryWrapper<ProductRuleRelation> qw = new QueryWrapper<>();
        qw.eq("rule_id", productRuleRelation.getRuleId());
        qw.eq("cate_id", productRuleRelation.getCateId());
        if(productRuleRelation.getCateChildId()!= null){
            qw.eq("cate_child_id", productRuleRelation.getCateChildId());
        }
        qw.last("limit 1");
        ProductRuleRelation relation = productRuleRelationService.getOne(qw);
        if(relation != null){
            boolean bl = productRuleRelationService.removeById(relation.getId());
            return toAjax(bl ? 1 : 0);
        }
        return toAjax(0);
    }

}
